//
//  TableViewController.m
//  TableView
//
//  Created by Carl_fang on 15/3/26.
//  Copyright (c) 2015年 Carl_fang. All rights reserved.
//

#import "TableViewController.h"
#import "HotMovieModel.h"
#import "TableViewCell.h"
#import "MovieDetailView.h"


@interface TableViewController ()<NSURLConnectionDataDelegate,NSURLConnectionDelegate>
{
    NSArray *array1;
    NSURLConnection *urlConnection;
}
@property (nonatomic, retain) NSMutableArray *array;
@property (nonatomic, retain) NSMutableData *recevieData;
@property (nonatomic, retain) NSMutableArray *mutableArray1;
//@property (nonatomic, retain) TableViewCell *cell;

@end

@implementation TableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _mutableArray1 = [NSMutableArray array];
    
//    self.tableView.backgroundColor = [UIColor whiteColor];
    
    NSURL *url = [[NSURL alloc] initWithString:@"http://api.m.mtime.cn/PageSubArea/HotPlayMovies.api?locationId=292"];
    
    
    NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
    
   urlConnection = [NSURLConnection connectionWithRequest:request delegate:self];
    
//    [NSURLConnection sendAsynchronousRequest:request queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError)
//    {
//        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
//        NSLog(@"dict = %@", dict);
//    }];
    
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    //NSLog(@"numberOfRowsInSection");
    
    return _array.count;
    
    
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    
    MovieDetailView *movieDetailVC = [[MovieDetailView alloc] init];
    HotMovieModel *model = _array[indexPath.row];
    movieDetailVC.movieID = model.movieId;
    
    //NSLog(@"%@",self.navigationController);
    [self.navigationController pushViewController:movieDetailVC animated:YES];
    
    
    

    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *ID = @"cell";
    
    TableViewCell *cell  = [tableView dequeueReusableCellWithIdentifier:ID];
    if (cell == nil) {
        
        cell = [[TableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }
    
    
    HotMovieModel *hotMovieModel = self.array[indexPath.row];
    cell.model = hotMovieModel;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    
    //cell.buy.tag = indexPath.row + 10000;
//    NSLog(@"%@",hotMovieModel.movieId);
    [self isHidden:hotMovieModel Cell:cell];
    //[cell.buy addTarget:self action:@selector(buyticket:) forControlEvents:UIControlEventTouchUpInside];
    
    
    return cell;
}


- (void)isHidden:(HotMovieModel *)model Cell:(TableViewCell *)cell
{
    if ([model.is3D intValue] == 0 )
    {
        cell.i3D.hidden = YES;
    }
    if ([model.isDMAX intValue] == 0) {
        
//        _i3D.frame = CGRectMake(65, 50, 30, 20);
        //cell.iMAX.frame = CGRectMake(65, 50, 30, 20);
        cell.iMAX.hidden = YES;
    }
    if ([model.isDMAX intValue] == 0) {
        cell.largeScreen.hidden = YES;
    }
}




- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    self.array = [NSMutableArray array];
   
    [self.recevieData appendData:data];
}


- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    
//    NSLog(@"%@", [NSThread currentThread]);
    
    NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:self.recevieData options:NSJSONReadingMutableContainers | NSJSONReadingMutableLeaves error:nil];
    NSArray *array = dict[@"movies"];
    for (NSDictionary *dict in array)
    {
        HotMovieModel *hotMovieModel = [HotMovieModel hotMovieModelWithDict:dict];
        [_array addObject:hotMovieModel];
    }
    
//    NSLog(@"connectionDidFinishLoading");
    
    [self.tableView reloadData];
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    self.recevieData = [NSMutableData data];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
//    NSLog(@"%@",error);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

//- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
//
//    // Return the number of sections.
//    return 0;
//}






/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
